home *** CD-ROM | disk | FTP | other *** search
/ Over 1,000 Windows 95 Programs / Over 1000 Windows 95 Programs (Microforum) (Disc 1).iso / 1244 / procapp4.c < prev    next >
C/C++ Source or Header  |  1996-06-17  |  5KB  |  206 lines

  1. #include <windows.h>
  2. #include "procbox.h"
  3. #include "papp4res.h"    // resource includes
  4.  
  5.  
  6. int CALLBACK _export PBCallback(HWND, int , LPARAM);
  7. BOOL CALLBACK _export PBDlgProc(HWND, UINT, WPARAM, LPARAM);
  8.  
  9. ////////////////////////////////////////////////////////////////////////////////
  10. //
  11. //    PROCAPP4.C    - Demonstrates use of Custom Process Box Interface
  12. //
  13. //                    - This demo shows how to make a simple Single-Callback
  14. //                        Modal Custom Process Box.
  15. //                    
  16. //                    - To use Custom Process Box with multiple callbacks or
  17. //                        as a modeless dialog (using the low-level interface),
  18. //                        you should look at PROCAPP2.C and PROCAPP3.C.  
  19. //                        (Replace calls to CreateProcessBox with 
  20. //                                        CreateCustomProcessBox.)
  21. //
  22. //                    - Contains code for 
  23. //                            - WinMain Entry Point
  24. //                            - WndProc
  25.     
  26. LRESULT FAR PASCAL _export WndProc(HWND, UINT, WPARAM, LPARAM);
  27.  
  28.                
  29. char        szAppName[] = "PROCAPP4";
  30. char        szWindowText[] = "ProcessBox - Modal Custom Process Box Demo";
  31.                            
  32.  
  33. /////////////////////////////////////////////////
  34. //
  35. //
  36. //     WndProc     
  37. //
  38.  
  39. LRESULT FAR PASCAL _export WndProc (HWND hwnd, UINT message, 
  40.                                                 WPARAM wParam, LPARAM lParam)
  41. {  
  42.     FARPROC    lpfnPBCallback;
  43.     FARPROC    lpfnPBDlgProc;
  44.     char    szText[64];          
  45.     HINSTANCE    hinst;
  46.     
  47.     switch (message)
  48.     {
  49.         case WM_KEYDOWN:                
  50.         case WM_LBUTTONDOWN:                                      
  51.             hinst = GetWindowWord(hwnd, GWW_HINSTANCE);
  52.            lpfnPBCallback = MakeProcInstance((FARPROC)PBCallback, hinst);    
  53.            lpfnPBDlgProc = MakeProcInstance((FARPROC)PBDlgProc, hinst);
  54.             
  55.             wsprintf(szText, "Returned: %d",
  56.                     CustomProcessBox(hinst, 
  57.                                         MAKEINTRESOURCE(IDD_CUSTPROCBOX),
  58.                                         hwnd, 
  59.                                         lpfnPBDlgProc,
  60.                                         lpfnPBCallback,
  61.                                         NULL));            // user data
  62.             
  63.             MessageBox(hwnd, szText, NULL, NULL);
  64.             
  65.             FreeProcInstance(lpfnPBCallback);    // free the callback thunk
  66.             FreeProcInstance(lpfnPBDlgProc);        // free the dialog box thunk
  67.         return 0;        
  68.                 
  69.         case WM_DESTROY:
  70.             PostQuitMessage(0);
  71.         return 0;        
  72.     }
  73.    return DefWindowProc (hwnd, message, wParam, lParam );
  74. }
  75. ///////////////////////////////////////////////////////////
  76. //
  77. //    The Custom Process Box Dialog Callback Procedure
  78. //
  79. //
  80. //
  81. BOOL CALLBACK _export PBDlgProc(HWND hwndPB, UINT message, WPARAM wParam, LPARAM lParam)
  82. {
  83.     switch (message)
  84.     {
  85.         case WM_INITDIALOG:
  86.         return TRUE;
  87.         
  88.         case WM_COMMAND:
  89.         switch (wParam)
  90.         {
  91.             case IDC_STOPPROCESS:
  92.                 SetCancelState(hwndPB, TRUE);        // stop the process.  This will kill destroy this dialog.                
  93.                 return 0;
  94.         }
  95.         return 0;
  96.         
  97.         case PM_SETGAUGE:
  98.             SetDlgItemInt(hwndPB, IDC_PERCENT, wParam, FALSE);
  99.         return 0;
  100.     }
  101.     return 0;
  102.     
  103. }
  104. ///////////////////////////////////////////////////////////
  105. //
  106. //    The Process Box Callback
  107. //
  108. //      
  109. int CALLBACK _export PBCallback(HWND hwndPB, int iCode, LPARAM lParam)
  110. {
  111.     static int    i;
  112.     int            iPercent;
  113.     static int    iPercentOld;
  114.     long    j;
  115.             
  116.     switch (iCode)
  117.     {
  118.         case PBC_OPEN:            
  119.             // allocate memory here
  120.             i=0;
  121.         return TRUE;    
  122.         
  123.         case PBC_CLOSE:
  124.             // free memory here
  125.         return TRUE;
  126.         
  127.         case PBC_CANCEL:
  128.         return (    MessageBox(hwndPB, "Really cancel operation?", "Message Box", 
  129.                         MB_APPLMODAL|MB_YESNO)==IDYES ? TRUE : FALSE );
  130.                             
  131.             
  132.         case PBC_PROCESS:
  133.             for (j=0; j<0xEFF; j++);    // processing ! 
  134.             i++;
  135.                         
  136.             if (i>10000)
  137.                 return PBCR_END;
  138.             
  139.             if (i==5000)
  140.                 SetWindowText(hwndPB, "Reached halfway");
  141.             
  142.             iPercent = i/100;
  143.             
  144.             if (iPercent != iPercentOld)
  145.             {  
  146.                 iPercentOld = iPercent;
  147.                 SendMessage(hwndPB, PM_SETGAUGE, iPercent, 0l);            
  148.             }
  149.             return PBCR_CONTINUE;
  150.                         
  151.     }
  152.     return TRUE;
  153. }
  154.  
  155. /////////////////////////////////////////////////
  156. //
  157. //
  158. //     WinMain - Entry Point
  159. //
  160. //
  161. int PASCAL WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  162.                     LPSTR lpszCmdParam, int nCmdShow)
  163. {     
  164.        
  165.     MSG             msg ;
  166.     WNDCLASS        wndclass ;       
  167.     HWND                hwnd;
  168.         
  169.     if (!hPrevInstance) 
  170.     {
  171.         wndclass.style         = NULL ;
  172.         wndclass.lpfnWndProc   = WndProc ;
  173.         wndclass.cbClsExtra    = 0 ;
  174.         wndclass.cbWndExtra    = 0 ;
  175.         wndclass.hInstance     = hInstance ;
  176.         wndclass.hIcon         = LoadIcon (NULL,IDI_APPLICATION) ;
  177.         wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  178.         wndclass.hbrBackground = GetStockObject (WHITE_BRUSH) ;
  179.         wndclass.lpszMenuName  = NULL ;
  180.         wndclass.lpszClassName = szAppName ;
  181.                 
  182.         RegisterClass (&wndclass) ;
  183.     }
  184.         
  185.     hwnd = CreateWindow (    szAppName,             // class
  186.                                      szWindowText,            // window text
  187.                                     WS_OVERLAPPEDWINDOW,            // style
  188.                                     CW_USEDEFAULT, CW_USEDEFAULT,        // x, y start
  189.                                     CW_USEDEFAULT, CW_USEDEFAULT,        // width, height
  190.                                     NULL,             // parent window handle
  191.                                     NULL,             // menu handle
  192.                                     hInstance,         // instance handle
  193.                                     NULL) ;            // long pointer to creation data
  194.  
  195.     ShowWindow (hwnd, nCmdShow) ;
  196.     UpdateWindow (hwnd) ;
  197.            
  198.     while (GetMessage (&msg, NULL, 0, 0))
  199.     {
  200.         TranslateMessage (&msg) ;
  201.         DispatchMessage (&msg) ;
  202.     }
  203.     
  204.     return msg.wParam ;   
  205. }  
  206.